﻿using DeviceCommons.DataHandling;
using DeviceCommons.DeviceMessages.Abstractions;
using DeviceCommons.DeviceMessages.Models.V1;

namespace DeviceCommons.DeviceMessages.Serialization.V2.Parsers
{
    public class DeviceMessageInfoReadingStatesParser : AbstractMessageParser<IDeviceMessageInfoReadingStates>, IDeviceMessageInfoReadingStatesParser
    {
        public DeviceMessageInfoReadingStatesParser() : base(new DeviceMessageInfoReadingStates())
        {
        }

        public override IDeviceMessageInfoReadingStates Parser(ReadOnlySpan<byte> bytes)
        {
            byte count = bytes[0];
            IDeviceMessageInfoReadingStates model = new DeviceMessageInfoReadingStates();
            model.StateArray = new IDeviceMessageInfoReadingState[count];
            var bytesTemp = bytes[1..];
            for (int i = 0; i < count; i++)
            {
                model.StateArray[i] = DeviceMessageSerializerProvider.InfoReadingStateV2Par.Parser(bytesTemp);
                bytesTemp = bytesTemp[model.StateArray[i].DataLength..];
            }
            Model = model;
            return model;
        }
    }
}
